/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: function_executor.cpp
 *
 * Purpose: like thread pool, but they are different, this implementation
 *          some like create a thread set to run special function.
 *
 * Developer:
 *   wen.gu , 2019-08-27
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#include "collie/core/function_executor.h"

#include <thread>
#include <vector>

#define LOG_TAG "Fexe"
#include "collie/core/log.h"

namespace collie
{
namespace core
{
/******************************************************************************
 **    MACROS
 ******************************************************************************/


/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/

using ThreadArray = std::vector<std::thread*>;

/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/

class FunctionExecutor::impl
{
public:
    ~impl();

public: 
    void run(uint32_t idx);
    void createThreads();
    void destroyThreads();
public:
    FuncMethod mMethod = nullptr;
    bool mIsRunning = false;
    ThreadArray mThds;
    std::mutex mLock;
};

FunctionExecutor::impl::~impl()
{
    /** todo something */
    destroyThreads();
}

void FunctionExecutor::impl::run(uint32_t idx)
{
    LOGI("function executor: %d, is starting\n", idx);
    while (mIsRunning)
    {
        if (mMethod)
        {
            mIsRunning = mMethod();
        }
    }

    LOGI("function executor: %d, is exit\n", idx);
}

void FunctionExecutor::impl::createThreads()
{
    mIsRunning = true;

    for (uint32_t i = 0; i < mThds.size(); i++)
    {
        mThds[i] = new std::thread(&impl::run, this, i);
    }
}

void FunctionExecutor::impl::destroyThreads()
{
    mIsRunning = false;
    for (uint32_t i = 0; i < mThds.size(); i++)
    {
        std::thread* thd = mThds[i];
        thd->join();
        delete thd;
    }

    mThds.clear();
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////


FunctionExecutor::FunctionExecutor()
    :mImpl(new impl)
{

}

FunctionExecutor::~FunctionExecutor()
{
    stop();
}

CollieErrc FunctionExecutor::setFunction(FuncMethod func, uint32_t executorCnt)
{
    mImpl->mMethod = func;
    mImpl->mThds.assign(executorCnt, nullptr);
    return CollieErrc::OK;
}

CollieErrc FunctionExecutor::start()
{
    AutoLock al(mImpl->mLock);
    if (mImpl->mIsRunning == false)
    {        
        mImpl->createThreads();
        return CollieErrc::OK;
    }

    return CollieErrc::InvalidStatus;
}

CollieErrc FunctionExecutor::stop()
{
    AutoLock al(mImpl->mLock);
    if (mImpl->mIsRunning)
    {         
        mImpl->destroyThreads();
        return CollieErrc::OK;
    }

    return CollieErrc::InvalidStatus;
}

} /** namespace core */
} /** namespace collie */
